package org.example.io.protocol.command;

import io.netty.channel.Channel;
import org.apache.commons.lang3.StringUtils;
import org.example.io.protocol.packet.CreateGroupRequestPacket;
import org.example.io.util.LogUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class CreateGroupCommand implements ConsoleCommand{

    private static final String SPLIT_STR = ",";
    @Override
    public void exec(Scanner scanner, Channel channel) {
        LogUtil.info("请输入群名称：");
        String groupName = scanner.next();
        while (StringUtils.isBlank(groupName)) {
            LogUtil.info("群名称不能为空，请重新输入：");
            groupName = scanner.next();
        }
        LogUtil.info("请输入要加入群里的用户ID，用逗号进行分隔：");
        String userIds = scanner.next();
        List<String> userList = Arrays.asList(userIds.split(SPLIT_STR));

        CreateGroupRequestPacket packet = new CreateGroupRequestPacket();
        packet.setGroupName(groupName);
        packet.setUserIds(userList);
        channel.writeAndFlush(packet);
    }

    @Override
    public String commandType() {
        return "createGroup";
    }
}
